﻿#region Using

using System.Collections.Generic;
using System.Linq;
using Vk.Methods.Users;

#endregion

namespace Vk.Methods.Messages
{
    /// <summary>
    ///     История LongPoll сервера
    /// </summary>
    public class LongPollServerHistory
    {
        public int[][] History { get; set; }

        /// <summary>
        ///     Список сообщений
        /// </summary>
        public List<Message> Messages { get; set; }

        public List<User> Profiles { get; set; }

        public LongPollServerHistory(ApiNode node)
        {
            var items = node.SelectNodes("history/*");
            var apiNodes = items as IList<ApiNode> ?? items.ToList();
            if (items != null && apiNodes.Any())
            {
                History = new int[apiNodes.Count()][];
                for (var i = 0; i < apiNodes.Count() - 1; i++)
                {
                    History[i] =
                        apiNodes[i].ChildNodes
                                   .Select(x => x.Int())
                                   .Where(x => x.HasValue)
                                   .Select(x => x.Value)
                                   .ToArray();
                }
            }

            var messages = node.SelectNodes("messages/*");
            if (messages != null && messages.Any())
            {
                Messages = messages.Select(x => new Message(x)).ToList();
            }
        }
    }
}